#include "em.h"
#include "../gui.menu.h"
#include "../gui.text.h"
#include "../gui.button.h"
#include "../gui.browser.h"

TOB::UI_EM::UI_EM(UIConfig &c): Mod(c) {
}

TOB::UI_EM::~UI_EM() {
}

void TOB::UI_EM::run() {
  // Connect
  Email em(config.nsip, config.username, config.password);
  this->em = &em;

  // Define ui
  UIMenu menum(0, 1, config.offsetX, 0);
  UIWindow statusw(0, 0, 0, 1);
  UIWindow textw(config.offsetX, 0, 0, 0);

  // Assign temp objects
  this->statusw = &statusw;
  this->textw = &textw;

  // Back
  menum.insert(config.lang["back"], [&](){ 
    menum.running = false; 
  });

  // Show mail list
  menum.insert(config.lang["em-list"], [&]() {
    // Prepare UI
    uint32 count = 0;
    UIMenu mailm(config.offsetX, 1, 0, 0);
    mailm.drawRest = false;
    std::string header;
    String tmp, tmp2;
    mailm.insert(config.lang["back"], [&](){
      mailm.running = false;
    });

    // Receive mail
    while(em.getNextSubject(count, header) == Code::Okay) {
      StringStream stream(CONVERT(header));
      getline(stream, tmp2);
      getline(stream, tmp);
      String tmp3 = tmp2 + config.lang["delim"] +  tmp;
      mailm.insert(tmp3, [&](){
        showLetter(mailm.getSelected() - 1);
        mailm.print();  
        mailm.showCursor(false);
      });
      ++count;
    }

    // Start
    if(!count) {
      status(config.lang["em-nomail"]);
    } else {
      status(config.lang["em-list"]);
      mailm.focus();
    }
  });

  // Compose message
  menum.insert(config.lang["em-compose"], [&](){
    // Init UI
    uint32 width = 15;
    UIMenu subm(config.offsetX, 1, width, 0);
    UIWindow titlesw(config.offsetX + width, 1, 10, 2);
    UIText recepientt(config.offsetX + width + 8, 1, 0, 1);
    UIText subjectt(config.offsetX + width + 8, 2, 0, 1);
    UIText bodyt(config.offsetX + width, 3, 0, 0);
    bodyt.multiline = true;

    // Fill titles
    titlesw.setCursor(0, 0);
    titlesw.putString(config.lang["em-recepient"]);
    titlesw.setCursor(0, 1);
    titlesw.putString(config.lang["em-subject"]);

    // Back
    subm.insert(config.lang["back"], [&](){ subm.running = false; });

    // Edit
    subm.insert(config.lang["enter"], [&](){
      recepientt.focus();
      subjectt.focus();
      bodyt.focus();
      subm.showCursor(false);
    });

    // Send
    subm.insert(config.lang["em-send"], [&](){
      std::string header = CONVERT(recepientt.lines[0]) + '\n' +
        CONVERT(subjectt.lines[0]);
      String _body = bodyt.getText();
      std::string body = CONVERT(_body);
      if(em.sendLetter(header, body) == Code::Okay) {
        status(config.lang["em-sent"]);
      } else {
        status(config.lang["em-not-sent"]);
      }    
    });

    // Tab events
    recepientt.eventTab = [&]() {
      recepientt.running = false;
      subjectt.focus();
    };
    subjectt.eventTab = [&]() {
      subjectt.running = false;
      bodyt.focus();
    };
    bodyt.eventTab = [&]() {
      bodyt.running = false;
    };

    // Run
    subm.focus();
  });

  menum.insert(config.lang["em-expand"], [&](){
    if(em.expandMail() == Code::Okay) {
      status(config.lang["em-expanded"]);
    } else {
      status(config.lang["em-not-expanded"]);
    }
  });

  String str;
  status(str);
  menum.focus();
}


// Set status
void TOB::UI_EM::status(String &msg) {
  statusw->setCursor(0, 0);
  statusw->clearLine();
  statusw->setColor(Color::Title);
  String out = config.lang["e-mail"] + config.lang["delim"] + msg;
  statusw->putString(out);
}


// Set progress
void TOB::UI_EM::progress(Float percent) {
  String count;
  StringStream stream;
  stream << percent;
  stream >> count;
  String temp = count + config.lang["percent"];
  status(temp);
}


// Show letter
void TOB::UI_EM::showLetter(uint32 letter) {
  UIBrowser browserb(config.offsetX, 1, 0, 0, config.ws_code);
  std::string body;
  em->readLetter(letter, body);
  browserb.setPage(body);
  browserb.focus();
}
